using Microsoft.EntityFrameworkCore;
using tafei_thingsapi.Models;

namespace tafei_thingsapi
{
    public class tafei_thingsapiContext : DbContext
    {
        public tafei_thingsapiContext(DbContextOptions<tafei_thingsapiContext> options) : base(options) { }

        public DbSet<Device> Devices { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<DeviceShare> DeviceShares { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 创建默认管理员账号
            modelBuilder.Entity<User>().HasData(
                new User
                {
                    Id = 1,
                    Username = "admin",
                    Password = "admin123", // 注意: 实际项目中应使用加密密码
                    Role = "Admin"
                });

            // 配置 DeviceShare 的复合唯一键
            modelBuilder.Entity<DeviceShare>()
                .HasIndex(ds => new { ds.DeviceId, ds.SharedUserId })
                .IsUnique();
        }
    }
}